package com.example.fmgidsprototype.data;

import java.io.Serializable;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.fmgidsprototype.model.Category;
import com.example.fmgidsprototype.model.Review;
import com.example.fmgidsprototype.model.Venue;
import com.example.fmgidsprototype.model.VenueLocation;

public class DataManagerImpl implements DataManager, Serializable{
	
	private Context context;
	private SQLiteDatabase db;
	private VenueDao venueDao;
	private CategoryDao categoryDao;
	private VenueCategoryDao venueCategoryDao;
	private VenueLocationDao venueLocationDao;
	private VenueReviewDao venueReviewDao;
	
	public DataManagerImpl(Context context){
		this.context = context;
		SQLiteOpenHelper helper = new OpenHelper(this.context);
		this.db = helper.getWritableDatabase();
		
		venueDao = new VenueDao(db);
		categoryDao = new CategoryDao(db);
		venueCategoryDao = new VenueCategoryDao(db);
		venueLocationDao = new VenueLocationDao(db);
		venueReviewDao = new VenueReviewDao(db);
	}
	
	private void openDb(){
		if(!this.db.isOpen()){
			this.db = SQLiteDatabase.openDatabase(DataConstants.DB_PATH, null, 
					SQLiteDatabase.OPEN_READWRITE);
			venueDao = new VenueDao(db);
			categoryDao = new CategoryDao(db);
			venueCategoryDao = new VenueCategoryDao(db);
			venueLocationDao = new VenueLocationDao(db);
			venueReviewDao = new VenueReviewDao(db);
		}
	}
	
	private void closeDb(){
		if(this.db.isOpen())
			this.db.close();
	}
	
	public List<Venue> getAllVenuesWithLocations(){
		return venueDao.getAllVenuesWithLocations();
	}

	@Override
	public Venue getVenue(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Venue> getAllVenues() {
		return venueDao.getAll();
	}

	@Override
	public List<Venue> getAllVenuesForCategory(long catId){
		return venueDao.getAllVenuesForCategory(catId);
	}

	@Override
	public Category getCategory(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long findCategory(String name) {
		return categoryDao.findCategory(name);
	}

	@Override
	public List<Category> getAllCategories() {
		return categoryDao.getAll();
	}

	
	public long saveVenue(Venue venue){
		return venueDao.save(venue);
	}
	
	public void saveCategories(List<Category> list){
		for(int i = 0; i < list.size(); i++){
			Category category = (Category)list.get(i);
			categoryDao.save(category);
		}
	}
	
	public long saveCategory(Category category){
		return categoryDao.save(category);
	}
	
	public void deleteAll(){
		categoryDao.deleteAll();
		venueDao.deleteAll();
		venueCategoryDao.deleteAll();
		venueLocationDao.deleteAll();
	}
	
	public long findVenue(long jsonId){
		return venueDao.findVenue(jsonId);
	}
	
	public long saveKey(VenueCategoryKey key){
		return venueCategoryDao.saveKey(key);
	}

	@Override
	public long saveVenueLocation(VenueLocation venueLocation) {
		return venueLocationDao.save(venueLocation);
	}

	@Override
	public VenueLocation getLocationFromVenue(long venueId) {
		return venueLocationDao.getLocationFromVenue(venueId);
	}

	@Override
	public List<Review> getReviewsForVenue(long venueId) {
		return venueReviewDao.getReviews(venueId);
	}

	@Override
	public long saveReview(Review review) {
		return venueReviewDao.save(review);
	}
	
	public List<Venue> getAllVenuesWithCatLocRev(){
		List<Venue> venues = this.getAllVenuesWithLocations();
		for(Venue venue : venues){
			List<Review> reviews = this.getReviewsForVenue(venue.getJsonId());
			venue.setReviews(reviews);
		}
		return venues;
	}

	@Override
	public long updateVenue(Venue venue) {
		return venueDao.update(venue);
	}

	@Override
	public long updateCategory(Category category) {
		return categoryDao.update(category);
	}

	@Override
	public long updateKey(VenueCategoryKey key) {
		VenueCategoryKey newKey = 
				venueCategoryDao.getKeyByVenueAndCategory(key.getVenueId(), key.getCategoryId());
		if(newKey == null)
			return -1;
		else
			return venueCategoryDao.updateKey(newKey);
	}

	@Override
	public long updateVenueLocation(VenueLocation venueLocation) {
		return venueLocationDao.update(venueLocation);
	}

	@Override
	public long updateReview(Review review) {
		return venueReviewDao.update(review);
	}
	
}
